Skip to content

DEV-1372: a disconnect while a queued command is about to be sent no longer crashes the app - #143

Merged
JongChern merged 2 commits into
masterfrom
DEV-1372_write_after_disconnect
Oct 29, 2026
Merged

JongChern merged 2 commits into
masterfrom
DEV-1372_write_after_disconnect

Conversation

@JongChern

Copy link
Copy Markdown
Contributor

The crash

If disconnect() lands while the driver's IO thread is about to send a queued command over Bluetooth Classic, the app process dies:

FATAL EXCEPTION: IO Thread D8:47:8F:04:BE:69
java.lang.NullPointerException: Attempt to invoke direct method 'void com.shimmerresearch.android.Shimmer$ConnectedThread.write(byte[])' on a null object reference
    at com.shimmerresearch.android.Shimmer.write(Shimmer.java:1237)
    at com.shimmerresearch.android.Shimmer.writeBytes(Shimmer.java:1703)
    at com.shimmerresearch.bluetooth.ShimmerBluetooth$IOThread.processNextInstruction(ShimmerBluetooth.java:765)

How it happens:

  • IOThread.processNextInstruction() takes a command off the queue, sleeps 10-110 ms, then calls writeBytes(), which is Shimmer.write() here.
  • disconnect() calls stop() on the caller's thread. stop() nulls mConnectedThread without waiting for the IO thread.
  • A disconnect inside that sleep makes write() dereference null. Nothing on the IO thread catches the exception, so Android kills the app.

This has been possible since 2024, when the guarded write() was commented out (first shipped in AndroidAPI_v3.0.84beta). It does not come from a driver change.

The Android HIL has hit it on drivers 0.11.9 to 0.11.12_beta. An app that connects and then disconnects straight away can hit it too: the connect sequence leaves SET_RWC_COMMAND queued after the unit is reported ready.

Not affected:

  • BLE: Shimmer3BLEAndroid writes through BleManager, which reports a failed write through a callback.
  • Java PC driver: ShimmerPC.writeBytes() null-checks the port and catches NullPointerException.

The fix

  • write() reads the connection once. If there is none, it drops the write and logs "Command NOT transmitted, not connected", the same way a write that fails on a closed socket is already handled.
  • mConnectedThread is now volatile, so the IO thread sees the null that stop() sets.
  • New test AAPI_00007_WriteAfterDisconnectTest writes after stop(), after disconnect(), and with no connection. All three tests fail with the NullPointerException on master and pass with the fix.

HIL harness (second commit)

The harness is what was disconnecting inside that window. Two changes:

Configure wait. configureAccelOnly() treated the configuration as done once it saw a "Ready" and the requested rate. Straight after a stopStreaming(), both arrive while commands are still queued: the "Ready" comes from the stop itself, and the rate is set by the first config chunk. samplingRates then disconnected under the IO thread. The wait now also needs the instruction queue to be empty. The configure queues the whole operation before it returns, so an empty queue means it has finished.

SD-logging scenarios check dock and USB.

  • A docked unit, or a Shimmer3R with USB plugged in, ACKs a start of SD logging and then ignores it (LogAndStream v1.01.002 on Shimmer3, v1.01.004 on Shimmer3R). The driver marks the unit logging on that ACK.
  • So midStreamSet and inStream were passing without logging anything, and connectReady failed later, on the reconnect.
  • All three now stop at once if the unit reports itself docked or on USB, and say why.
  • The README lists this under preparing each unit.

Testing

  • Unit tests: :ShimmerAndroidInstrumentDriver:testDebugUnitTest, 29 tests, all pass. :shimmerHilTest:compileDebugAndroidTestJavaWithJavac builds.
  • Android HIL on this branch: driver 0.11.12_beta, a Shimmer3 over Classic and a Shimmer3R over BLE.
    • The crash race happened during the run. In connectReady over Classic, the test disconnected 15 ms before the IO thread sent the queued SET_RWC_COMMAND. write() logged the dropped write and the app kept running. No logcat in the run has a FATAL EXCEPTION.
    • Every samplingRates configure now finishes with 0 instructions queued, and samplingRates passes on both units.
    • Both units were docked or on USB, so midStreamSet, inStream and connectReady stopped at the new check, with the new message.
    • The other 8 scenario runs passed.
  • Still needed: a full gate run with both units off the dock and USB, on master after merge.

🤖 Generated with Claude Code

JongChern and others added 2 commits October 30, 2026 07:52
The driver's IO thread takes a queued command, sleeps 10-110 ms, then
sends it through writeBytes(), which is Shimmer.write() here.
disconnect() calls stop() on the caller's thread, and stop() nulls
mConnectedThread without waiting for the IO thread. So a disconnect
inside that sleep made write() dereference null. Nothing on the IO
thread catches that, and Android killed the app. The Android HIL hit it
over Bluetooth Classic on drivers 0.11.9 to 0.11.12_beta. The bug has
been there since 2024, when the guarded write() was commented out.

write() now reads the connection once, and drops and logs a write with
none, as a write that fails on a closed socket already is. The field is
volatile so the IO thread sees stop()'s null. AAPI_00007 covers a write
after stop(), after disconnect() and before any connection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s check dock and USB

configureAccelOnly() counted the configuration done on a "Ready" plus the
requested rate. Straight after a stopStreaming(), the stop's own "Ready"
and the rate from the first config chunk both arrived with commands
still queued. samplingRates then disconnected under the IO thread, which
is how the run hit the crash fixed in the previous commit. The wait now
also needs an empty instruction queue. The configure queues the whole
operation before it returns, so an empty queue means it has finished.

Docked, or with USB in a Shimmer3R, the firmware ACKs a start of SD
logging and then ignores it. The driver marks the unit logging on that
ACK, so midStreamSet and inStream passed without logging anything, and
connectReady failed later, on the reconnect. All three now fail at once
when the unit reports itself docked or on USB, and say why. The README
lists it under preparing each unit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JongChern
JongChern merged commit d197184 into master Oct 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant